package com.atguigu.srb.core.service.impl;

import com.atguigu.srb.core.enums.TransTypeEnum;
import com.atguigu.srb.core.hfb.FormHelper;
import com.atguigu.srb.core.hfb.HfbConst;
import com.atguigu.srb.core.hfb.RequestHelper;
import com.atguigu.srb.core.mapper.TransFlowMapper;
import com.atguigu.srb.core.mapper.UserInfoMapper;
import com.atguigu.srb.core.pojo.entity.TransFlow;
import com.atguigu.srb.core.pojo.entity.UserAccount;
import com.atguigu.srb.core.mapper.UserAccountMapper;
import com.atguigu.srb.core.pojo.entity.UserInfo;
import com.atguigu.srb.core.service.UserAccountService;
import com.atguigu.srb.core.util.LendNoUtils;
import com.atguigu.srb.mq.pojo.dto.SmsDTO;
import com.atguigu.srb.mq.service.MqService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 用户账户 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2022-09-13
 */
@Service
public class UserAccountServiceImpl extends ServiceImpl<UserAccountMapper, UserAccount> implements UserAccountService {

    @Resource
    UserInfoMapper userInfoMapper;
    @Resource
    TransFlowMapper transFlowMapper;
    @Resource
    MqService mqService;
    @Override
    public String commitCharge(BigDecimal chargeAmt, Long userId) {

        //获取投资人的bindCode

        UserInfo userInfo = userInfoMapper.selectById(userId);
        String bindCode = userInfo.getBindCode();
        Map<String, Object> map = new HashMap<>();

        map.put("agentId",HfbConst.AGENT_ID);
        map.put("agentBillNo", LendNoUtils.getChargeNo());
        map.put("bindCode",bindCode);
        map.put("chargeAmt",chargeAmt);
        map.put("freeAmt",new BigDecimal("0"));
        map.put("notifyUrl",HfbConst.RECHARGE_NOTIFY_URL);
        map.put("returnUrl",HfbConst.RECHARGE_RETURN_URL);
        map.put("timestamp", RequestHelper.getTimestamp());
        String sign = RequestHelper.getSign(map);
        map.put("sign",sign);

        String form = FormHelper.buildForm(HfbConst.RECHARGE_URL, map);
        return form;
    }

    @Override
    public void notifyCommitCharge(Map<String, Object> map) {
        /*回调函数 修改userAccount表中的值*/
        String chargeAmt =(String) map.get("chargeAmt");
        String agentBillNo = (String)map.get("agentBillNo");

        //根据bindCode 查询Userinfo表 获得userId
        String bindCode = (String) map.get("bindCode");
        QueryWrapper<UserInfo> userInfoQueryWrapper = new QueryWrapper<>();
        userInfoQueryWrapper.eq("bind_code",bindCode);
        UserInfo userInfo = userInfoMapper.selectOne(userInfoQueryWrapper);
        Long userInfoId = userInfo.getId();
        //修改userAccount中的 amount字段的值 = 当前值+充值/消费的值 freeze_amount字段的值

        UpdateWrapper<UserAccount> userAccountUpdateWrapper
                =new UpdateWrapper<>();
        userAccountUpdateWrapper.setSql("amount = amount +"
                +chargeAmt+", freeze_amount = freeze_amount+"+0)
        .eq("user_id",userInfoId);
        baseMapper.update(null,userAccountUpdateWrapper);

        //trans_flow 交易流水表中添加交易流水的记录
        TransFlow transFlow = new TransFlow();
        transFlow.setMemo(TransTypeEnum.RECHARGE.getTransTypeName());
        transFlow.setTransAmount(new BigDecimal(chargeAmt));
        transFlow.setTransNo(agentBillNo);
        transFlow.setTransType(TransTypeEnum.RECHARGE.getTransType());
        transFlow.setUserId(userInfoId);
        transFlow.setUserName(userInfo.getName());
        transFlow.setTransTypeName(TransTypeEnum.RECHARGE.getTransTypeName());
        transFlowMapper.insert(transFlow);

        //调用消息队列rabbitMQ给用户发送短信
        SmsDTO smsDTO = new SmsDTO();
        smsDTO.setMessage("ok");
        smsDTO.setMobile(userInfo.getMobile());
        mqService.sendMessage("exchange.topic.sms","routing.sms.item",smsDTO);
    }

    @Override
    public BigDecimal getUserAccount(Long userId) {
        QueryWrapper<UserAccount> userAccountQueryWrapper = new QueryWrapper<>();
        userAccountQueryWrapper.eq("user_id",userId);
        UserAccount userAccount = baseMapper.selectOne(userAccountQueryWrapper);
        BigDecimal amount = userAccount.getAmount();
        return amount;
    }
}
